/*
Philip Dilse
dilsep
Assignment 3

unit test for remodel card
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "dominion.h"
#include "dominion_helpers.h"

int main() {
    int i, j;
    int maxHand = 100;
    int discardSize = 5;
    int player = 0;
    int expectedReturn;
    int effectReturn;
    int handPos;
    int choice1, choice2;
    int goldCount, remodelCount, choice2Count;
    struct gameState *gs = newGame();

    gs->whoseTurn = player; 
    // set up random number gen
    srand(time(NULL));

    for (i = 1; i <= maxHand; i++) {
        expectedReturn = 0;

        // randomly assign hand cards, set hand count, also keep track how many of each card
        gs->handCount[player] = i;
        for (j = 0; j < i; j++) {
            gs->hand[player][j] = rand() % (treasure_map + 1);
        }       
 
        // assign 5 cards to discard and set discard count 
        gs->discardCount[player] = discardSize;
        for (j = 0; j < discardSize; j++) {
            gs->discard[player][j] = gold; 
        }

        // swap remodel card into random hand position and set to handPos
        handPos = rand() % gs->handCount[player];
        gs->hand[player][handPos] = remodel;        

        // randomly pick choice1 from hand
        choice1 = rand() % gs->handCount[player];

        // if choice 1 == handPos set expectedReturn to -1, can't trash the played remodel card
        if (choice1 == handPos)
            expectedReturn = -1;

        // randomly pick choice2 from all cards
        choice2 = rand() % (treasure_map + 1);

        // if choice2 costs more than 2 more than choice 1 set expectedReturn to -1
        if (getCost(choice2) > getCost(choice1) + 2)
            expectedReturn = -1;

        // run cardEffect()
        effectReturn = cardEffect(remodel, choice1, choice2, 0, gs, handPos, &j);

        //delete
        if (i == 1)
            printf("expected=%d actual=%d\n", expectedReturn, effectReturn);
        // if expected != effect return print error
        if (expectedReturn != effectReturn)
            printf("remodel_effect error: handcount=%d expected=%d actual=%d handPos=%d choice1=%d choice2=%d\n",
                    i, expectedReturn, effectReturn, handPos, choice1, choice2);
        else {
            // else discard count should be 7 and should contain a remodel, choice2, and 5 gold
            goldCount = remodelCount = choice2Count = 0;

            for (j = 0; j < gs->discardCount[player]; j++) {
                if (gs->discard[player][j] == gold)
                    goldCount++;
                else if (gs->discard[player][j] == remodel)
                    remodelCount++;
                else if (gs->discard[player][j] == choice2)
                    choice2Count++;
            }

            if (gs->discardCount[player] != discardSize + 2 || goldCount != 5 || remodelCount != 1 || choice2Count != 1)
                printf("remodel_effect() error: bad discard, handcount=%d, discardcount=%d, gold=%d, remodel=%d, choice2=%d\n",
                        i, gs->discardCount[player], goldCount, remodelCount, choice2Count); 

            // also check hand count
            if (i > 1 && i - 2 != gs->handCount[player])
                printf("remodel_effect() error: bad hand, handcountbefore=%d handcountafter=%d\n", i, gs->handCount[player]); 
        }
    }

    free(gs);
    return 0;
}
